package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.damain.Category;
import com.itheima.reggie.damain.Dish;
import com.itheima.reggie.damain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.krb5.internal.rcache.DflCache;

import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public Page findByPage(Integer pagenum, Integer pageSize, String name) {
//设置分页查询
        Page<Dish>page=new Page<>(pagenum,pageSize);
        //设置业务条件
        LambdaQueryWrapper<Dish>wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //执行查询
        page=dishMapper.selectPage(page,wrapper);
        //遍历分页列表，得到商品信息
        List<Dish>dishList=page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish:dishList
                 ) {
                Category category=categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
                //查询口味
                //。。。
            }
        }
        System.out.println(page);
        return page;
    }

    @Override
    public void save(Dish dish) {
        //保存基本信息
        dishMapper.insert(dish);//主键自动回显
        //遍历
        if (CollectionUtil.isNotEmpty(dish.getFlavors())){
            for (DishFlavor dishFlavor:dish.getFlavors()) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }


    @Override
    public Dish findByid(Long id) {
        //根据id从彩标中主键查询，获取菜品信息
        Dish dish=dishMapper.selectById(id);
        //根据菜品categoryid查询分类名
        Category category=categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        //根据菜品的id查口味
        LambdaQueryWrapper<DishFlavor>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor>dishFlavorList=dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);
        return dish;
    }
//修改
    @Override
    public void update(Dish dish) {
        //根据菜品id修改菜品信息
        dishMapper.updateById(dish);
        LambdaQueryWrapper<DishFlavor>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);

        //重新保存
        if (CollectionUtil.isNotEmpty(dish.getFlavors())){
            for (DishFlavor flavor:dish.getFlavors()
                 ) {
                flavor.setDishId(dish.getId());
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        //1. 如果其中一个是在售, 就全部不允许删除
        //select count(1) from dish where status = '1' and id in #{ids}
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getStatus, 1);//在售
        wrapper.in(Dish::getId, ids);//id in #{ids}
        Integer count = dishMapper.selectCount(wrapper);
        if (count > 0) {
            throw new CustomException("当前菜品有处于在售状态的,不能删除");
        }

        if (CollectionUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                //2. 删除当前菜品的口味列表
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, id);
                dishFlavorMapper.delete(wrapper1);

                //3. 根据菜品id删除当前菜品信息
                dishMapper.deleteById(id);
            }
        }
    }

    @Override
    public void status(Integer status, List<Long> id) {
        LambdaQueryWrapper<Dish>wrapper=new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,id);
        Dish dish=new Dish();
        dish.setStatus(status);
        dishMapper.update(dish,wrapper);
    }
//对象接收参数，--失败
//    @Override
//    public void status(Dish dish) {
//        LambdaQueryWrapper<Dish>wrapper=new LambdaQueryWrapper<>();
//        wrapper.in(Dish::getId,dish.getId());
//        Dish dish1=new Dish();
//        dish1.setStatus(dish.getStatus());
////                .eq(Dish::getStatus,dish.getStatus());
//        dishMapper.update(dish1,wrapper);
//    }
}
